var article_id, target_name, target_comment_id;
/**
 * 点赞
 */
function giveLike(e) {
    $(e).toggleClass("like-active-icon");
    let articleId = $(e).parent().parent().prev().text();
    let userName = $("#loginedUserName").text().trim();
    if (userName == null || userName == "") {
        window.location.href = "login";
    }
    console.info("文章ID：" + articleId);
    $.ajax({
        type: "POST",
        url: "like/" + userName + "/" + articleId,
        success: function (result) {
            let resultJson = jQuery.parseJSON(result);
            console.info(result);
            $.ajax({
                type:"GET",
                url:"refresh/like/" + articleId,
                success: function (data) {
                    console.info(data);
                    $(e).next().empty();
                    $(e).next().html(data);
                    if (resultJson.flag === "OK") {
                        xtip.msg("点赞成功！");
                        let $num = $(e).children("span");
                        $num.text(parseInt($num.text()) + 1);
                    } else if (resultJson.flag === "DELETE") {
                        xtip.msg("取消点赞！");
                        let $num = $(e).children("span");
                        $num.text(parseInt($num.text()) - 1);
                    } else {
                        xtip.msg("点赞失败！");
                    }
                }
            });
        },
        error: function () {

        }
    });

}

/**
 * 显示发布评论
 */
function showPostComment(e) {
    let userName = $("#loginedUserName").text().trim();
    if (userName == null || userName == "") {
        window.location.href = "login";
    } else {
        $(e).parent().parent().parent().next().toggleClass("d-none");
        $(e).parent().parent().parent().next().next().toggleClass("d-none");
    }
}
var currentPage = 2;
/**
 * 加载更多文章
 */
function loadMore(e) {
    let userName = $(e).prev().text();
    $.ajax({
        type: "GET",
        url: "load/more?currentPage="+currentPage +"&userName="+userName,
        success: function (result) {
            currentPage += 1;
            let flag = "<div></div>";
            // console.info("接收的值：" + result.replace(/\s/g,""));
            if(result.replace(/\s/g,"") === flag){
                xtip.msg("没有更多内容了！");
            }
            var $div = $("<div></div>");
            $div.html(result);
            $("#newsfeed-items-grid").append($div.children());
        }
    });
}

/**
 * 显示回复评论框
 */
function showComment(e, flag) {
    console.info($(e));
    target_name = $(e).parent().children("div").children("div").children("a").text().trim();
    // 设置标题
    $("#reply-name").text("回复@" + target_name);

    if (flag === 0) {
        article_id = $(e).parent().parent().parent().find(".articleId").text().trim().trim();
        target_comment_id = $(e).parent().children("p.d-none").text().trim();
    } else {
        target_comment_id = $(e).parent().parent().parent().children().eq(0).text().trim();
        article_id = $(e).parent().parent().parent().parent().parent().find(".articleId").text().trim();
    }
    console.info("文章ID" + article_id + "目标评论ID：" + target_comment_id + "目标评论用户名: " + target_name);
    $('#commentModal').modal('show');

}

/**
 * 发表评论
 */
function postComment(e) {
    let userName = $("#loginedUserName").text();
    let commentContent = $(e).prev().children(".form-group")
        .children("textarea").val();
    let articleId = $(e).parent().parent().children(".articleId").text();
    console.info("用户名：" + userName + "评论内容：" + commentContent + "文章ID：" + articleId);
    $.ajax({
        type: "POST",
        url: "publish/comment/" + userName + "/" + commentContent + "/ " + articleId,
        success: function (result) {
            console.info(result);
            if (result === "OK") {
                xtip.msg("发表评论成功");
                let commentNum = $(e).parent().prev().children(".post-additional-info").children(".comments-shared").children().eq(0).children("span");
                // 评论数加1
                $(commentNum).text(parseInt($(commentNum).text()) + 1);
                $(e).prev().children(".form-group")
                    .children("textarea").val("");
                $(e).parent().addClass("d-none");
                // window.location.reload();
            }

        },
        error: function () {

        }

    })
}

function hideComment(e) {
    $(e).parent().addClass("d-none");
}

/**
 * 回复评论
 */
function publishReplyComment() {
    let commentContent = $("#comment-content").val();
    let userName = $("#loginedUserName").text().trim();
    console.info("文章ID" + article_id + "目标评论ID：" + target_comment_id + "目标评论用户名: " + target_name + " 当前用户：" + userName);
    $.ajax({
        type: "POST",
        url: "publish/reply/" + userName + "/ " + target_name + "/" + commentContent + "/" + article_id + "/" + target_comment_id,
        success: function (result) {
            console.info(result);
            if (result === "OK") {
                xtip.msg("回复成功，刷新可查看！");
                $("#comment-content").val("");
                $(e).parent().addClass("d-none");
                // window.location.reload();
            }

        }
    });
}

function deletePost(e) {
   let post_id = $(e).parent().parent().parent().parent().parent().parent().parent().prev().text().trim();
   $.ajax({
       type: "Post",
       url: "delete-post?articleId=" + post_id,
       success: function (result) {
            if(result === "OK"){
                xtip.msg("删除成功！");
                $(e).parent().parent().parent().parent().parent().parent().parent().parent().remove();
            }else{
                xtip.msg("删除失败！");
            }
       }
   });
}
function shareContent(e) {
    let post_id = $(e).parent().parent().parent().prev().text().trim();
    let userName = $("#loginedUserName").text().trim();
    $.ajax({
        type: "Post",
        url: "share?userName=" + userName +"&articleId=" + post_id,
        success: function (result) {
            if(result === "OK"){
                xtip.msg("转发分享成功！");
                $(e).parent().parent().parent().parent().parent().parent().parent().parent().remove();
            }else{
                xtip.msg("转发分享失败！");
            }
        }
    });
}